﻿#if NET40 || NET461

// global.ashx

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Routing;

namespace Apewer.Web
{

    /// <summary></summary>
    public class WebsiteGlobal : HttpApplication
    {

        /// <summary></summary>
        public WebsiteGlobal() { }

        /// <summary></summary>
        protected virtual void Application_Start(object sender, EventArgs e) { }

        /// <summary></summary>
        protected virtual void Application_End(object sender, EventArgs e) { }

        /// <summary></summary>
        protected virtual void Application_Error(object sender, EventArgs e) { }

        /// <summary></summary>
        protected virtual void Application_BeginRequest(object sender, EventArgs e) { }

        /// <summary></summary>
        protected virtual void Application_AuthenticateRequest(object sender, EventArgs e) { }

        /// <summary></summary>
        protected virtual void Session_Start(object sender, EventArgs e) { }

        /// <summary></summary>
        protected virtual void Session_End(object sender, EventArgs e) { }

        /// <summary>路由处理所有请求（包括指向文件的请求），与定义的模式匹配的所有请求都由路由处理。</summary>
        protected void AddRouter(bool value = true)
        {
            RouteTable.Routes.RouteExistingFiles = value;
        }

        /// <summary>添加路由。</summary>
        protected void AddRouter(string url, string path)
        {
            if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path)) return;
            RouteTable.Routes.MapPageRoute("", url, path, false);
        }

        /// <summary>添加路由。</summary>
        protected void AddRouter(Dictionary<string, string> rules)
        {
            // rules.Add("api", "~/api.aspx");
            // rules.Add("{app}/{function}/{*args}", "~/app.aspx");
            if (rules == null) return;
            foreach (var rule in rules)
            {
                AddRouter(rule.Key, rule.Value);
            }
        }

    }

}

#endif
